<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class OwnserviceModelAccount extends JModel
{
	/**
	 * Account data
	 *
	 * @var object
	 */
	var $_data = null;

	/**
	 * Constructor
	 *
	 * @since 1.5
	*/
	function __construct(){
		parent::__construct();

		$id = JRequest::getVar('id');
		$this->setId((int)$id);
	}

	/**
	 * Method to set the account id
	 *
	 * @access	public
	 * @param	int	account ID number
	 */
	function setId($id) {
		// Set new account ID and wipe data
		$this->_id		= $id;
		$this->_data	= null;
	}

	/**
	 * Overridden get method to get properties account the data
	 *
	 * @access	public
	 * @param	string	$property	The name of the property
	 * @param	mixed	$value		The value of the property to set
	 * @return 	mixed 				The value of the property
	 * @since	1.0
	 */
	function get($property, $default=null) {
		/*
		* Load the account data
		*/
		if ($this->_loadData()) {
			if(isset($this->_data->$property)) {
				return $this->_data->$property;
			}
		}
		return $default;
	}

	/**
	 * Method to get bank account data
	 *
	 * @access public
	 * @return object
	*/
	function &getData(){
		
		/*
		* Load the account data
		*/
		if($this->_loadData()) {
				// Someting code
		} else {

			$row = new stdClass();
			$row->id			= null;
			$row->user_id		= null;
			$row->bank_name		= null;
			$row->account_name	= null;
			$row->account_no	= null;
			$row->branch_name	= null;
			$row->branch_city	= null;
			$row->country		= 'Thailand';
			$row->address		= null;
			$row->email			= null;
			$row->city			= null;
			$row->state			= null;
			$row->postcode		= null;
			$row->phone_no		= null;

			$this->_data = $row;
		}

		return $this->_data;
	}

	/**
	 * Method to load required data
	 *
	 * @access	private
	 * @return	array
	 * @since	1.0
	*/
	function _loadData() {

		//Lets load the account if it doesn't already exist
		if(empty($this->_data)) {

			$user = & JFactory::getUser();

			$where = ' AND a.deleted=0 ';
			if($user->gid < 25){
				$canViewOwn = Ownservice::checkAccess('account','view','account','own');
				if($canViewOwn){
					$where .= ' AND a.user_id ='.$user->get('id');
				}
			}

			$query = ' SELECT a.*, u.username, u.name FROM #__ownservice_accounts  AS a '
					.' LEFT JOIN #__ownservice_users AS p ON p.user_id=a.user_id '
					.' LEFT JOIN #__users AS u ON u.id=p.user_id'
					.' WHERE a.id='.$this->_id
					.$where;

			$this->_db->setQuery($query);
			$this->_data = $this->_db->loadObject();

			return (boolean) $this->_data;
		}

		return true;
	}

	/**
	 * Method to store the customer account
	 *
	 * @access public
	 * @return boolean True on success
	*/
	function store($data) {

		//Check the request forgeries
		JRequest::checkToken() or jexit('Invalid Token');

		$user = & JFactory::getUser();

		$row = & $this->getTable( 'ownservice_account','' );

		if(!$row->bind($data)) {
			$this->setError( $this->_db->getErrorMsg() );
			return false;
		}

		// Are we saving from an customer account edit?
		if ($row->id) {
			$datenow =& JFactory::getDate();
			$row->modified 		= $datenow->toMySQL();
			$row->modified_by 	= $user->get('id');
		}

		$row->created_by 	= $row->created_by ? $row->created_by : $user->get('id');

		if ($row->created && strlen(trim( $row->created )) <= 10) {
			$row->created 	.= ' 00:00:00';
		}

		$config =& JFactory::getConfig();
		$tzoffset = $config->getValue('config.offset');
		$date =& JFactory::getDate($row->created, $tzoffset);
		$row->created = $date->toMySQL();

		// Make sure the data is valid
		if (!$row->check()) {
			$this->setError($row->getError() );
			return false;
		}

		// Store it in the db
		if (!$row->store()) {
			$this->setError( $this->_db->getErrorMsg() );
			return false;
		}

		return true;
	}

	/**
	 * Method to get countries
	 *
	 * @access public
	 * @return object
	*/
	function getCountries(){

		$query = ' SELECT * FROM #__ownservice_countries ORDER BY country';

		$this->_db->setQuery($query);
		$countries = $this->_db->loadObjectList();

		return $countries;
	}

	/**
	 * Method to fetch members according to a given mask
	 * 
	 * @return object
	 * @since 1.0
	 */
	function getmembers($mask="")
	{
		$user		=&JFactory::getUser();
		$ownuser	=& Ownservice::getUser();
	
		$where		=array();
		$where[]	='u.block = 0';
		
		if($ownuser->get('branch_id')){
			$where[]	='p.branch_id='.$ownuser->get('branch_id');
		} else {
			//check level 
			if( ($user->gid < 25) && ($ownuser->get('level_id') < 5) ){
				$where[]	='p.branch_id= 0';
			}
		}
		
		if($mask!="")
		{
			$where[] = "(u.name like '%$mask%' OR u.username like '%$mask%')";
		}
		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );
		
		$query = 'SELECT u.* FROM #__users AS u '
				.' LEFT JOIN #__ownservice_users AS p ON p.user_id=u.id '
				.$where
				.' GROUP BY u.id '
				.' ORDER BY u.name';

		$this->_db->setQuery($query);
		$users = $this->_db->loadObjectlist();
		
		return $users;
	}

	/**
	 * Method get user
	 *
	 * @return object
	 * @since 1.5
	*/
	function getUser($id) {

		$query = ' SELECT p.*, u.name FROM #__ownservice_users AS p '
				.' LEFT JOIN #__users AS u ON u.id=p.user_id '
				.' WHERE u.id='.$id.' AND u.block = 0 ';
		$this->_db->setQuery($query);
		$profile = $this->_db->loadObject();

		return $profile;
	}
}